//
//  NSBundle+LBAddition.m
//  LBFoundation
//
//  Created by WuShiHai on 19/5/24.
//  Copyright (c) 2019 Lebbay. All rights reserved.
//

#import "NSBundle+LBAddition.h"

@implementation NSBundle (MFAddition)

@end

@implementation NSBundle (MFConfiguration)

+ (NSString *)stringValueWithConfigurationService:(NSString *)service
                                              key:(NSString *)key
                                     defaultValue:(NSString *)defaultValue {
   return [self valueWithConfigurationService:service
                                          key:key
                                 defaultValue:defaultValue
                                         type:[NSString class]];
}

+ (BOOL)boolValueWithConfigurationService:(NSString *)service
                                      key:(NSString *)key
                             defaultValue:(BOOL)defaultValue {
    return [[self valueWithConfigurationService:service
                                            key:key
                                   defaultValue:@(defaultValue)
                                           type:[NSNumber class]] boolValue];
}

+ (NSDictionary *)mapValueWithConfigurationService:(NSString *)service
                                               key:(NSString *)key
                                      defaultValue:(NSString *)defaultValue {
    return [self valueWithConfigurationService:service
                                           key:key
                                  defaultValue:defaultValue
                                          type:[NSDictionary class]];
}

+ (id)valueWithConfigurationService:(NSString *)service
                                key:(NSString *)key
                       defaultValue:(id)defaultValue
                               type:(Class)type {
    id value = [self valueWithConfigurationService:service
                                                   key:key
                                          defaultValue:defaultValue];
    if ([value isKindOfClass:type]) {
        return value;
    }else{
        return nil;
    }
}

+ (id)valueWithConfigurationService:(NSString *)service
                                key:(NSString *)key
                       defaultValue:(id)defaultValue {
    NSDictionary *configurationMaps = [[NSBundle mainBundle]
                                       objectForInfoDictionaryKey:@"LBConfiguration"];
    if ([configurationMaps isKindOfClass:[NSDictionary class]]) {
        NSDictionary *serviceMaps = [configurationMaps objectForKey:service];
        if ([serviceMaps isKindOfClass:[NSDictionary class]]) {
            NSString *value = [serviceMaps objectForKey:key];
            if (value) {
                return value;
            }
        }
    }
    
    return defaultValue;
}

+ (NSBundle *)bundleWithClass:(Class)cClass
                     resource:(NSString *)resource {
    if (!cClass || !resource || resource.length <= 0) {
        return nil;
    }
    NSBundle *bundle = [NSBundle bundleForClass:cClass];
    NSURL *url = [bundle URLForResource:resource withExtension:@"bundle"];
    return [NSBundle bundleWithURL:url];
}

@end
